package one.stand.controller;

import com.google.common.collect.Lists;
import com.jmp.api.crm.dto.*;
import com.jmp.autocofiguration.web.interceptor.SecurityAuthCheck;
import com.jmp.base.dto.CardListDto;
import com.jmp.base.dto.CardListVo;
import com.jmp.feign.CardFeign;
import io.swagger.annotations.ApiOperation;
import one.stand.dto.ConvertUtils;
import one.stand.dto.MenuTreeDto;
import one.stand.dto.PageResponse2;
import one.stand.model.Company;
import one.stand.model.ResultModel;
import one.stand.service.CompanyBaseService;
import one.stand.service.user.MenuService;
import one.stand.util.AssertUtil;
import one.stand.util.OneListUtil;
import one.stand.util.RequestContextHolder;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("crmPc/user/menu")
public class UserMenuController {
    @Autowired
    private CardFeign cardFeign;

    @Autowired
    private MenuService menuService;
    @Autowired
    private CompanyBaseService companyBaseService;

    @ApiOperation(value = "用户列表")
    @PostMapping("list")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel<UserMenuListResponse> list(@RequestAttribute UserMenuListRequest request) {
        final Integer userCompanyId = RequestContextHolder.getRequestContext().getUser().getCompanyId();

        ResultModel<CardListVo> cardListVoResultModel = cardFeign.list(CardListDto.builder()
                .companyId(userCompanyId)
                .keyword(request.getName())
                .build());
        AssertUtil.isTrue(cardListVoResultModel);

        Company company = companyBaseService.getById(userCompanyId);
        PageResponse2<UserMenuListResponse.User> userPageResponse2 = ConvertUtils.page2(cardListVoResultModel.getData().getCardPage(), businessCard -> UserMenuListResponse.User.builder()
                .cardId(businessCard.getCardId())
                .userId(businessCard.getUserId())
                .name(businessCard.getName())
                .phone(businessCard.getPhone())
                .boss(BooleanUtils.toInteger(businessCard.getUserId().equals(company.getUserId())))
                .build());

        UserMenuListResponse response = UserMenuListResponse.builder()
                .users(userPageResponse2)
                .build();
        return ResultModel.success(response);
    }

    @ApiOperation(value = "用户权限")
    @PostMapping("info")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel<UserMenuInfoResponse> info(@RequestAttribute UserMenuInfoRequest request) {
        List<MenuTreeDto> menuTreeDtos = menuService.getAll(null, MenuService.TYPE_CRM_PC);
        List<Integer> menuOpen = menuService.getOpen(request.getUserId());

        List<UserMenuInfoResponse.Menu> menus = ConvertUtils.list(menuTreeDtos, menuTreeDto -> {

            List<Integer> userOpen = Lists.newArrayList();
            List<UserMenuInfoResponse.Menu> childList = null;
            if (OneListUtil.isNotEmpty(menuTreeDto.getMenus())) {
                childList = Lists.newArrayListWithExpectedSize(menuTreeDto.getMenus().size());
                for (MenuTreeDto childDto : menuTreeDto.getMenus()) {
                    if (menuOpen.contains(childDto.getMenu().getMenuId())) {
                        userOpen.add(childDto.getMenu().getMenuId());
                    }
                    childList.add(UserMenuInfoResponse.Menu.builder()
                            .menuId(childDto.getMenu().getMenuId())
                            .name(childDto.getMenu().getValue())
                            .desc(childDto.getMenu().getAdjective())
                            .build());
                }
            } else {
                childList = Lists.newArrayListWithExpectedSize(1);
                if (menuOpen.contains(menuTreeDto.getMenu().getMenuId())) {
                    userOpen.add(menuTreeDto.getMenu().getMenuId());
                }
                childList.add(UserMenuInfoResponse.Menu.builder()
                        .menuId(menuTreeDto.getMenu().getMenuId())
                        .name(menuTreeDto.getMenu().getValue())
                        .desc(menuTreeDto.getMenu().getAdjective())
                        .build());
            }
            boolean checkAll = OneListUtil.isNotEmpty(childList) && childList.size() == userOpen.size();
            return UserMenuInfoResponse.Menu.builder()
                    .checkAll(checkAll)
                    .menuId(menuTreeDto.getMenu().getMenuId())
                    .name(menuTreeDto.getMenu().getValue())
                    .desc(menuTreeDto.getMenu().getAdjective())
                    .userpermissions(userOpen)
                    .permissions(childList)
                    .build();
        });

        UserMenuInfoResponse response = UserMenuInfoResponse.builder()
                .permissions(menus)
                .build();
        return ResultModel.success(response);
    }

    @ApiOperation(value = "保存")
    @PostMapping("save")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel save(@RequestAttribute UserMenuSaveRequest request) {

        menuService.update(request.getUserId(), request.getRoleIds());

        return ResultModel.success();
    }
}
